knitr::opts_chunk$set(echo = T, message = F, warning = F)
library(tidyverse)
library(ggbeeswarm)
library(ggdist)
library(waffle)
library(fontawesome)

Generating data

p_data <- data.frame(x = c(rnorm(500, mean = .5, sd = 1), rnorm(500, mean = 0, sd = 1)),
                     group = factor(rep(c("With Intervention", "Without Intervention"), each = 500), levels = c("Without Intervention", "With Intervention")))

Normal Curves

Normal Curve (using stat_function())

Visualization of Cohen’s \(U_3\) with two normal curves.

ggplot(data = data.frame(x = c(-3, 3)), aes(x)) +
  stat_function(fun = dnorm, n = 101, args = list(mean = 0, sd = 1), 
                # geom = "area", 
                # fill = "#d0d4d4", 
                color = "#d0d4d4",
                size = 2) + 
  geom_vline(aes(xintercept = 0), color = "#d0d4d4", linetype = "dotted", size = 2) +
  geom_label(x=-2, y=.3, label="without\nintervention", color = 'white', fill = "#d0d4d4", fontface = "bold") +
  stat_function(fun = dnorm, n = 101, args = list(mean = .5, sd = 1),                     # effect size = .5
                xlim = c(-3, 0),
                # geom = "area", 
                # fill = "#ff857a", 
                color = "#ff857a", 
                size = 2) +
  stat_function(fun = dnorm, n = 101, args = list(mean = .5, sd = 1),                     # effect size = .5
                xlim = c(0, 3),
                geom = "area", 
                fill = "#cc6a61", 
                color = "#cc6a61", 
                alpha = .5,
                size = 2) + 
  geom_vline(aes(xintercept = .5), color = "white", linetype = "dotted", size = 1.5, alpha = .3) +    # effect size = .5
  geom_label(x=2.2, y=.3, label="with\nintervention", color = 'white', fill = "#ff857a", fontface = "bold") +
  geom_label(x=1, y=.15, label="69%", color = 'white', fill = "#ff857a", fontface = "bold") +
  scale_x_continuous(breaks = c(-2:+2), labels = c("- -", "-", "Test Score", "+", "++")) +
  scale_y_continuous(expand = c(0,0), limits = c(0,0.41)) +
  theme_minimal() +
  theme(panel.grid = element_blank(),
        axis.ticks.x = element_line(color = "#666666"),
        axis.line.x = element_line(color = "#666666"),
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        axis.text.x = element_text(face = "bold"))

Normal Curve (using the simulated data)

ggplot(p_data, aes(x=x, group = group, fill = group, colour = group)) +
  geom_density(alpha = .7, bw = 1) +
  scale_fill_viridis_d() +
  scale_color_viridis_d() +
  geom_label(x=-2, y=.3, label="without\nintervention", color = 'white', fill = "#440154", fontface = "bold") +
  geom_label(x=2.2, y=.3, label="with\nintervention", color = '#090c0e', fill = "#fde725", fontface = "bold") +
  scale_y_continuous(expand = c(0,0)) +
  theme_minimal() +
  theme(legend.position = "none",
        panel.grid = element_blank(),
        axis.ticks.x = element_line(color = "#666666"),
        axis.line.x = element_line(color = "#666666"),
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        axis.text.x = element_text(face = "bold"))

Violin plot

ggplot(p_data%>%dplyr::filter(group == "Without Intervention"), aes(x=1, y = x, group = group, fill = group, colour = group)) +
  geom_violin(alpha = .7, bw = 1) +
  geom_violin(data = p_data%>%dplyr::filter(group == "With Intervention"), alpha = .7, bw = 1) +
  scale_fill_viridis_d() +
  scale_color_viridis_d() +
  geom_label(x=-2, y=.3, label="without\nintervention", color = 'white', fill = "#440154", fontface = "bold") +
  geom_label(x=2.2, y=.3, label="with\nintervention", color = '#090c0e', fill = "#fde725", fontface = "bold") +
  scale_y_continuous(expand = c(0,0)) +
  theme_minimal() +
  theme(legend.position = "none",
        panel.grid = element_blank(),
        axis.ticks.x = element_line(color = "#666666"),
        axis.line.x = element_line(color = "#666666"),
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        axis.text.x = element_text(face = "bold")) +
  coord_flip()

Beeswarm

geom_beeswarm()

Visualization of Cohen’s \(U_3\) with a beeswarm plot.

ggplot(p_data, aes(x=group, y = x, group = group, fill = group, colour = group)) +
  geom_beeswarm(cex = 1.6) +
  scale_fill_viridis_d() +
  scale_color_viridis_d() +
  geom_label(x=1.4, y=-2, label="without\nintervention", color = 'white', fill = "#440154", fontface = "bold") +
  geom_label(x=2.4, y=2, label="with\nintervention", color = '#090c0e', fill = "#fde725", fontface = "bold") +
  scale_y_continuous(expand = c(0,0)) +
  theme_minimal() +
  theme(legend.position = "none",
        panel.grid = element_blank(),
        axis.ticks.x = element_line(color = "#666666"),
        axis.line.x = element_line(color = "#666666"),
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        axis.text.x = element_text(face = "bold")) +
  coord_flip()

ggplot(p_data%>%dplyr::filter(group == "Without Intervention"), aes(x = 1, y = x)) +
  geom_beeswarm(cex = 1.2, alpha = .5, fill = "#440154", color = "#440154", size = 3) +
  geom_beeswarm(data = p_data%>%dplyr::filter(group == "With Intervention"), cex = 1.2, alpha = .5, fill = "#fde725", color = "#fde725", size = 3) +
  scale_fill_viridis_d() +
  scale_color_viridis_d() +
  geom_label(x=1.4, y=-2, label="without\nintervention", color = 'white', fill = "#440154", fontface = "bold") +
  geom_label(x=2.4, y=2, label="with\nintervention", color = '#090c0e', fill = "#fde725", fontface = "bold") +
  scale_y_continuous(expand = c(0,0)) +
  theme_minimal() +
  theme(legend.position = "none",
        panel.grid = element_blank(),
        axis.ticks.x = element_line(color = "#666666"),
        axis.line.x = element_line(color = "#666666"),
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        axis.text.x = element_text(face = "bold")) +
  coord_flip()

Dotplot

stat_dots()

ggplot(p_data, aes(x=x, group = group, fill = group, colour = group)) +
  stat_dots(alpha = .5) +
  scale_fill_viridis_d() +
  scale_color_viridis_d() +
  geom_label(x=-2, y=.3, label="without\nintervention", color = 'white', fill = "#440154", fontface = "bold") +
  geom_label(x=2.2, y=.3, label="with\nintervention", color = '#090c0e', fill = "#fde725", fontface = "bold") +
  scale_y_continuous(expand = c(0,0)) +
  theme_minimal() +
  theme(legend.position = "none",
        panel.grid = element_blank(),
        axis.ticks.x = element_line(color = "#666666"),
        axis.line.x = element_line(color = "#666666"),
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        axis.text.x = element_text(face = "bold"))

stat_dots() with quantiles = 100

ggplot(p_data, aes(x=x, group = group, fill = group, colour = group)) +
  stat_dots(quantiles = 100, alpha = .4) +
    scale_fill_viridis_d() +
  scale_color_viridis_d() +
  geom_label(x=-2, y=.3, label="without\nintervention", color = 'white', fill = "#440154", fontface = "bold") +
  geom_label(x=2.2, y=.3, label="with\nintervention", color = '#090c0e', fill = "#fde725", fontface = "bold") +
  scale_y_continuous(expand = c(0,0)) +
  theme_minimal() +
  theme(legend.position = "none",
        panel.grid = element_blank(),
        axis.ticks.x = element_line(color = "#666666"),
        axis.line.x = element_line(color = "#666666"),
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        axis.text.x = element_text(face = "bold"))

stat_dotsinterval() with quantiles = 100

ggplot(p_data, aes(x=x, y = group, group = group, fill = group, colour = group)) +
  stat_dotsinterval(quantiles = 100) +
  scale_fill_viridis_d() +
  scale_color_viridis_d() +
  geom_label(x=-2, y=1.5, label="without\nintervention", color = 'white', fill = "#440154", fontface = "bold") +
  geom_label(x=2.2, y=2.5, label="with\nintervention", color = '#090c0e', fill = "#fde725", fontface = "bold") +
  scale_y_discrete(expand = c(0,0)) +
  theme_minimal() +
  theme(legend.position = "none",
        panel.grid = element_blank(),
        axis.ticks.x = element_line(color = "#666666"),
        axis.line.x = element_line(color = "#666666"),
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        axis.text.x = element_text(face = "bold"))

stat_dots() different color

p_data %>%
ggplot(aes(x=x, y = group, group = group, fill = stat(x>0), colour = stat(x>0))) +
  stat_dots() +
  scale_fill_viridis_d() +
  scale_color_viridis_d() +
  # geom_label(x=-2, y=1.5, label="without\nintervention", color = 'white', fill = "#440154", fontface = "bold") +
  # geom_label(x=2.2, y=2.5, label="with\nintervention", color = '#090c0e', fill = "#fde725", fontface = "bold") +
  scale_y_discrete(expand = c(0,0)) +
  theme_minimal() +
  theme(legend.position = "none",
        panel.grid = element_blank(),
        axis.ticks.x = element_line(color = "#666666"),
        axis.line.x = element_line(color = "#666666"),
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        axis.text.x = element_text(face = "bold"))

Histograms

geom_histogram()

ggplot(data = p_data%>%dplyr::filter(group == "Without Intervention"), aes(x=x)) +
  geom_histogram(alpha = .5, fill = "#440154", color = "#440154") +
  geom_histogram(data = p_data%>%dplyr::filter(group == "With Intervention"), alpha = .5, fill = "#fde725", color = "#fde725") +
  scale_fill_viridis_d() +
  scale_color_viridis_d() +
  geom_label(x=-2, y=35, label="without\nintervention", color = 'white', fill = "#440154", fontface = "bold") +
  geom_label(x=2.2, y=35, label="with\nintervention", color = '#090c0e', fill = "#fde725", fontface = "bold") +
  scale_y_discrete(expand = c(0,0)) +
  theme_minimal() +
  theme(legend.position = "none",
        panel.grid = element_blank(),
        axis.ticks.x = element_line(color = "#666666"),
        axis.line.x = element_line(color = "#666666"),
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        axis.text.x = element_text(face = "bold"))

Pie Charts

Donut Plots

Percent of participants scoring higher than the control group’s average.

p_data %>%
  mutate(x0 = ifelse(x>0,1,0)) %>%
  group_by(group) %>%
  summarize(x0 = mean(x0)) %>%
  ggplot(aes(y = x0, x = group, group = group, fill = group, colour = group)) +
    geom_col() +
    coord_polar(theta = "y") +
    scale_y_reverse(limits = c(1,0), labels = NULL) +
    scale_x_discrete(limits = c(" ", "Without Intervention", "With Intervention")) +
    scale_fill_viridis_d() +
    scale_color_viridis_d() +
    geom_label(x=2.2, y=.085, label="without\nintervention", color = 'white', fill = "#440154", fontface = "bold") +
    geom_label(x=3.1, y=.05, label="with\nintervention", color = '#090c0e', fill = "#fde725", fontface = "bold") +
    theme_void() +
    theme(legend.position = "none",
          panel.grid = element_blank(),
          axis.ticks.x = element_blank(),
          axis.line.x = element_blank(),
          axis.title.y = element_blank(),
          axis.title.x = element_blank(),
          axis.text.y = element_blank(),
          axis.text.x = element_text(face = "bold"))

Waffle Charts

Percent of participants scoring higher than the control group’s average.

p_data %>%
  mutate(x0 = ifelse(x>0,1,0)) %>%
  group_by(group, x0) %>%
  summarize(vals = n()) %>%
  ggplot(aes(fill = as.factor(x0), values = vals)) +
    geom_waffle(n_rows = 20, radius = unit(4, "pt")) +
    scale_fill_viridis_d() +
    scale_color_viridis_d() +
    scale_x_continuous(labels = NULL) +
    coord_equal() +
    # geom_label(x=2.2, y=.085, label="without\nintervention", color = 'white', fill = "#440154", fontface = "bold") +
    # geom_label(x=3.1, y=.05, label="with\nintervention", color = '#090c0e', fill = "#fde725", fontface = "bold") +
    theme_void() +
    theme(legend.position = "none",
          panel.grid = element_blank(),
          axis.ticks.x = element_blank(),
          axis.line.x = element_blank(),
          axis.title.y = element_blank(),
          axis.title.x = element_blank(),
          axis.text.y = element_blank(),
          axis.text.x = element_text(face = "bold")) +
  facet_wrap(~group)